package cn.tedu.smartos.baseuser.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class MailService {

    @Autowired
    private JavaMailSender mailSender;  // Spring Boot自动配置的邮件发送器

    /**
     * 生成随机验证码
     * @return 6位数字验证码
     */
    public String generateCode() {
        Random random = new Random();
        int code = random.nextInt(900000) + 100000; // 生成100000-999999之间的随机数
        return String.valueOf(code);
    }

    /**
     * 发送简单文本邮件
     * @param to 收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("204760637@qq.com"); // 发件人(需与配置的username一致)
        message.setTo(to);        // 收件人
        message.setSubject(subject); // 主题
        message.setText(content);    // 内容
        mailSender.send(message);
    }
}
